package yunwo.cn.yzstation.utils;



import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * String相关工具类
 * Created by joial on 2016/2/26.
 */
public class StringUtil {
    private final static String bankNumberMatchStr = ".{4}(?!$)";
    private final static String blackSpaceStr = "$0 ";
    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     *
     * @param input
     * @return boolean
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input) || "null".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
    // 判断手机号是否合
    public static boolean isPhoneNumber(String phoneNumber) {
        // ^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
        String phoneReg = "^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0&6-8])|(14[5&7]))\\d{8}$";
        Pattern p = Pattern.compile(phoneReg);
        Matcher m = p.matcher(phoneNumber);
        return m.matches();
    }
//    // 判断手机号是否合
//    public static boolean isPhoneNumberNew(String phoneNumber) {
//        //"^((0\\d{2,3}-\\d{7,8})|(1[3584]\\d{9}))$"
//        // /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$)/
//        String phoneReg = "^((0\\\\d{2,3}-\\\\d{7,8})|(1[3584]\\\\d{9}))$|^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0&6-8])|(14[5&7]))\\d{8}$";
//        Pattern p = Pattern.compile(phoneReg);
//        Matcher m = p.matcher(phoneNumber);
//        return m.matches();
//    }
    public static boolean isCheckMobiPhone(String billId){
        String reg="(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +
                "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";
        Pattern pat = Pattern.compile(reg);
        Matcher mat = pat.matcher(billId);
        return mat.matches();
    }


    /**
     * 验证密码是否在6~16位之间
     *
     * @param str
     * @return
     */
    public static boolean isPasswordMatches(String str) {
        if (str.length() > 5 && str.length() < 17) {
            return true;
        }
        return false;
    }

    // 判断是否为数字
    public static boolean isNum(String str) {
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }

    /**
     * 获取带星的银行号码,并且每四位一空格(仅后四位显示)
     *
     * @param acctNo
     * @return
     */
 /*   public static String getBankStarNumber(String acctNo) {
        String star = "";
        for (int i = 0; i <= acctNo.length() - 5; i++) {
            star += "*";
        }
        return StringUtils.overlay(acctNo, star, 0, acctNo.length() - 4)
                .replaceAll(bankNumberMatchStr, blackSpaceStr);
    }*/

    /**
     * 获取带星的名字(两位时：姓隐藏，否则第二个字符隐藏)
     *
     * @param acctName
     * @return
     */
 /*   public static String getStarName(String acctName) {
        String str = "";

        if (acctName.length() <= 2) {
            str = acctName;
        } else {
            str = StringUtils.overlay(acctName, "*", 1, 2);
        }
        return str;
    }*/

    // 验证身份证号是否合法
    public static boolean isIDCardNumber(String iDCardNumber) {

        // String iDCardReg="/(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)/";
        String iDCardReg = "^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";

        Pattern p = Pattern.compile(iDCardReg);
        Matcher m = p.matcher(iDCardNumber);
        return m.matches();
    }

    // 判断是否为中文
    public static boolean isZH(String name) {
        String zhReg = "[\u4e00-\u9fa5]{2,}";
        Pattern p = Pattern.compile(zhReg);
        Matcher m = p.matcher(name);
        return m.matches();
    }
    /**
     * 校验车牌号
     *
     * @param plateNumber
     * @return
     */
    public static boolean checkPlateNumber(String plateNumber) {
        String chk1 = "^[\u4e00-\u9fa5]{1}[A-Z_0-9]{6}$";
        String chk2 = "^[A-Z_0-9]{5}[\u4e00-\u9fa5]{1}$";
        String chk3 = "^[A-Z]{2}[A-Z_0-9]{7}$";
        String chk4 = "^[A-Z]{2}[-]{1}[0-9]{5}$";
        String chk5 = "^[A-Z]{2}[0-9]{5}$";
        String chk6 = "^[A-Z]{2}[A-Z_0-9]{5}$";
        String chk7 = "^[A-Z]{2}[\u4e00-\u9fa5]{1}[A-Z_0-9]{4}$";
        String chk8 = "^[A-Z]{2}[0-9]{8}$";
        String chk9 = "^[A-Z]{2}[\u4e00-\u9fa5]{1}[A-Z_0-9]{5}$";
        String chk10 = "^[A-Z]{2}[\u4e00-\u9fa5]{1}[0-9]{5}$";
        String chk11 = "^[A-Z]{2}[0-9]{5}$";
        String chk12 = "^[A-Z]{2}[0-9]{7}$";
        String chk13 = "^[A-Z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}$";
        String chk14 = "^[\u4e00-\u9fa5]{1}[A-Z_0-9]{5}[\u4e00-\u9fa5]{1}$";
        String[] chk = new String[] { chk1, chk2, chk3, chk4, chk5, chk6, chk7,
                chk8, chk9, chk10, chk11, chk12, chk13, chk14 };
        for (int i = 0; i < chk.length; i++) {
            Pattern pat = Pattern.compile(chk[i]);
            Matcher mat = pat.matcher(plateNumber);
            if (mat.matches()) {
                return true;
            }
        }
        return false;
    }
    public static String insertStrToStr(String insertStr, String tagUrl) {
        String _url = "";
        if (tagUrl.endsWith(".jpg")) {
            String[] splitStr = tagUrl.split(".jpg");
            _url = splitStr[0] + insertStr + ".jpg";
        } else if (tagUrl.endsWith(".png")) {
            String[] splitStr = tagUrl.split(".png");
            _url = splitStr[0] + insertStr + ".png";
        } else if (tagUrl.endsWith(".gif")) {
            String[] splitStr = tagUrl.split(".gif");
            _url = splitStr[0] + insertStr + ".gif";
        } else if(tagUrl.endsWith(".jpeg")) {
            String[] splitStr = tagUrl.split(".jpeg");
            _url = splitStr[0] + insertStr + ".jpeg";
        }
        return _url;
    }
    // 替换、过滤特殊字符
    public static String StringFilter(String str) throws PatternSyntaxException {
        str = str.replaceAll("【", "[").replaceAll("】", "]")
                .replaceAll("！", "!");// 替换中文标号
        String regEx = "[『』]"; // 清除掉特殊字符
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    //将null 替换成 ""
    public static String replaceNullToString(String str){
        if(StringUtil.isEmpty(str)){
            str = "";
        }
        return str;
    }

}
